#include "lvgl/lvgl.h"
#include "../ui.h"
#include <string.h>
#include <stdio.h>
#include <time.h>  
#include "../tcp_client.h" 
#include"../app_data.h"
// 字体声明
LV_FONT_DECLARE(lv_font_fan_24);  // 自定义字体


// 全局变量：当前选中的科室
const char *g_selected_dept = "内科";
// 全局预约信息初始化（默认空值）
ReservationInfo g_reservation = {
    .dept = "",
    .doctor = "",
    .date_raw = "",
    .date_show = "",
    .time = "",
    .remain_ticket = 0
};
// -------------------------- 1. 核心数据定义 --------------------------
// 医生排班结构体
typedef struct {
    const char *date;    // 日期（格式：YYYY-MM-DD）
    const char *time;    // 时段（如：上午 08:30-10:00）
    int remain_ticket;   // 余票数量
} DoctorSchedule;

// 医生信息结构体
typedef struct {
    const char *name;        // 医生姓名
    const char *title;       // 职称
    const char *intro;       // 医生简介
    DoctorSchedule schedules[3]; // 最多3个排班时段
} DoctorInfo;

// 内科医生数据库
static DoctorInfo internal_medicine_doctors[] = {
    {
        .name = "张建国",
        .title = "主治医师",
        .intro = "擅长高血压、冠心病等心血管疾病，从医15年",
        .schedules = {
            {"2025-09-27", "上午 08:30-10:00", 3},
            {"2025-09-28", "下午 14:00-15:30", 1},
            {"2025-09-29", "上午 09:00-10:30", 5}
        }
    },
    {
        .name = "刘芳",
        .title = "副主任医师",
        .intro = "专注消化系统疾病（胃炎、胃溃疡），患者好评率98%",
        .schedules = {
            {"2025-09-27", "上午 10:00-11:30", 2},
            {"2025-09-28", "下午 15:00-16:30", 4},
            {"2025-09-29", "上午 08:30-10:00", 6}
        }
    },
    {
        .name = "王浩",
        .title = "主治医师",
        .intro = "擅长糖尿病、甲状腺疾病等内分泌疾病诊治",
        .schedules = {
            {"2025-09-28", "上午 09:30-11:00", 1},
            {"2025-09-29", "下午 14:30-16:00", 3},
            {"2025-09-30", "上午 10:00-11:30", 2}
        }
    }
};

// 可选择的日期列表（增加用户友好的显示名称）
typedef struct {
    const char *raw_date;  // 用于内部逻辑判断 (YYYY-MM-DD)
    const char *show_name; // 用于界面显示 (例如: 9.27 周六)
} DateItem;

static const DateItem selectable_dates[] = {
    {"2025-09-27", "9.27 周六"},
    {"2025-09-28", "9.28 周日"},
    {"2025-09-29", "9.29 周一"},
    {"2025-09-30", "9.30 周二"},
    {"2025-10-01", "10.1 周三"}
};
#define DATE_COUNT (sizeof(selectable_dates) / sizeof(selectable_dates[0]))

// -------------------------- 2. 全局UI对象 --------------------------
lv_obj_t *ui_Screen8;
static lv_obj_t *tab_date;       // “按日期挂号”Tab按钮
static lv_obj_t *tab_doctor;     // “按医生挂号”Tab按钮
static lv_obj_t *content_area;   // 内容展示区
static lv_obj_t *date_buttons[DATE_COUNT]; // 日期选择按钮数组
static char selected_date[20];   // 当前选中的日期 (YYYY-MM-DD)
static int current_mode = 0;     // 当前模式：0=按日期，1=按医生
static const char * mbox_btns[] = {"确定", NULL}; 

// 函数原型声明
void ui_Screen8_screen_init(void);
void ui_Screen8_screen_deinit(void);
static void tab_event_handler(lv_event_t *e);
static void back_btn_event_handler(lv_event_t *e);
static void create_date_mode_content(void);
static void create_doctor_mode_content(void);
static void populate_date_mode_list(const char *target_date);
static void update_tab_style(void);
static void clear_content_area(void);
static void date_btn_event_handler(lv_event_t *e); // 日期按钮事件
static void reserve_btn_event_cb(lv_event_t *e);   // 预约按钮事件


// -------------------------- 3. 事件回调函数 --------------------------
/** 日期按钮点击事件
 *        切换选中日期，并刷新医生列表
 */
static void date_btn_event_handler(lv_event_t *e) {
    lv_obj_t *btn = lv_event_get_target(e);
    int *date_index_ptr = (int *)lv_event_get_user_data(e);
    if (!date_index_ptr || *date_index_ptr < 0 || *date_index_ptr >= DATE_COUNT) return;

    int new_index = *date_index_ptr;

    // 1. 更新选中日期
    strncpy(selected_date, selectable_dates[new_index].raw_date, sizeof(selected_date) - 1);
    selected_date[sizeof(selected_date) - 1] = '\0';

    // 2. 更新所有日期按钮的样式，高亮当前选中的按钮
    for (int i = 0; i < DATE_COUNT; i++) {
        if (lv_obj_is_valid(date_buttons[i])) {
            if (i == new_index) {
                lv_obj_set_style_bg_color(date_buttons[i], lv_color_hex(0x1E88E5), 0);
                lv_obj_set_style_text_color(date_buttons[i], lv_color_white(), 0);
            } else {
                lv_obj_set_style_bg_color(date_buttons[i], lv_color_white(), 0);
                lv_obj_set_style_text_color(date_buttons[i], lv_color_hex(0x666666), 0);
            }
        }
    }

    // 3. 清空并重建内容区，以显示新日期的医生
    clear_content_area();
    create_date_mode_content();
}

/**
 * @brief 弹窗“确定”按钮回调：跳转至Screen9
 */
static void mbox_ok_btn_event_cb(lv_event_t *e) {
    // 1. 获取弹窗对象，点击确定后删除弹窗
    lv_obj_t *mbox = lv_event_get_current_target(e);
    if (lv_obj_is_valid(mbox)) {
        lv_obj_del(mbox); // 销毁弹窗
    }


    // 3. 初始化并切换到Screen9
    if (ui_Screen9 == NULL) { 
        ui_Screen9_screen_init();
    }
    update_reservation_display();
    lv_scr_load(ui_Screen9); // 切换到Screen9
}

 //预约按钮事件

static void reserve_btn_event_cb(lv_event_t *e) {
    // 1. 获取被点击的按钮和医生姓名
    lv_obj_t *btn = lv_event_get_target(e);
    const char *doc_name = (const char *)lv_obj_get_user_data(btn);
    if (!doc_name) doc_name = "未知医生";

    // 2. 查找当前选中日期的原始格式和显示格式
    const char *date_raw = "未知日期";
    const char *date_show = "未知日期";
    for (int i = 0; i < DATE_COUNT; i++) {
        if (strcmp(selectable_dates[i].raw_date, selected_date) == 0) {
            date_raw = selectable_dates[i].raw_date;
            date_show = selectable_dates[i].show_name;
            break;
        }
    }

    // 3. 查找当前医生在选中日期的具体就诊时段（关键：补充时段信息）
    const char *doc_time = "未知时段";
    int remain_ticket = 0;
    int doc_count = sizeof(internal_medicine_doctors) / sizeof(DoctorInfo);
    for (int i = 0; i < doc_count; i++) {
        DoctorInfo *doc = &internal_medicine_doctors[i];
        if (strcmp(doc->name, doc_name) != 0) continue; // 匹配当前医生
        
        // 匹配当前日期的时段
        for (int j = 0; j < 3; j++) {
            if (strcmp(doc->schedules[j].date, selected_date) == 0 && 
                doc->schedules[j].remain_ticket > 0) {
                doc_time = doc->schedules[j].time;
                remain_ticket = doc->schedules[j].remain_ticket;
                break;
            }
        }
        break;
    }

    // 4. 赋值全局预约信息
    strncpy(g_reservation.dept, g_selected_dept, sizeof(g_reservation.dept)-1);
    strncpy(g_reservation.doctor, doc_name, sizeof(g_reservation.doctor)-1);
    strncpy(g_reservation.date_raw, date_raw, sizeof(g_reservation.date_raw)-1);
    strncpy(g_reservation.date_show, date_show, sizeof(g_reservation.date_show)-1);
    strncpy(g_reservation.time, doc_time, sizeof(g_reservation.time)-1);
    g_reservation.remain_ticket = remain_ticket;



      // -------------------------- 新增：发送挂号信息到服务器 --------------------------
        if (client_socket != -1) 
        { // 检查客户端是否已连接服务器
            // 消息格式：APPOINT:科室:医生:显示日期:时段:余票
            char appoint_msg[256];
            snprintf(appoint_msg, sizeof(appoint_msg), 
                    "APPOINT:%s:%s:%s:%s:%d",
                    g_reservation.dept,
                    g_reservation.doctor,
                    g_reservation.date_show,
                    g_reservation.time,
                    g_reservation.remain_ticket);
            // 发送消息
            if (send(client_socket, appoint_msg, strlen(appoint_msg), 0) == -1) {
                printf("Screen8: 发送挂号信息失败，尝试重连...\n");
                // 连接断开时尝试重连并重新发送
                if (tcp_client_init() == 0) {
                    send(client_socket, appoint_msg, strlen(appoint_msg), 0);
                    printf("Screen8: 重连后发送挂号信息成功：%s\n", appoint_msg);
                }
            } else {
                printf("Screen8: 发送挂号信息成功：%s\n", appoint_msg);
            }
        }
        else 
        {
            // 客户端未连接，尝试初始化并发送
            if (tcp_client_init() == 0) {
                char appoint_msg[256];
                snprintf(appoint_msg, sizeof(appoint_msg), 
                        "APPOINT:%s:%s:%s:%s:%d",
                        g_reservation.dept,
                        g_reservation.doctor,
                        g_reservation.date_show,
                        g_reservation.time,
                        g_reservation.remain_ticket);
                send(client_socket, appoint_msg, strlen(appoint_msg), 0);
                printf("Screen8: 初始化客户端后发送挂号信息成功：%s\n", appoint_msg);
            }
        }

    // 5. 弹窗提示预约成功（原有逻辑保留）
    char msg[150];
    snprintf(msg, sizeof(msg), "预约成功！\n\n日期：%s\n医生：%s\n时段：%s\n\n请按时前往医院就诊。", 
             date_show, g_reservation.doctor, doc_time);
    
    lv_obj_t *mbox = lv_msgbox_create(lv_scr_act(), "预约成功", msg, mbox_btns, false);
    lv_obj_set_style_text_font(mbox, &lv_font_fan_24, 0);
    lv_obj_center(mbox);
    lv_obj_add_event_cb(mbox, mbox_ok_btn_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

}

/**
 * @brief 返回按钮事件
 */
static void back_btn_event_handler(lv_event_t *e) {
    if (ui_Screen7 == NULL) {
        ui_Screen7_screen_init(); // 假设上一屏初始化函数存在
    }
    lv_scr_load(ui_Screen7);
    lv_obj_del_delayed(ui_Screen8, 0);
    ui_Screen8 = NULL;
}

/**
 * @brief Tab切换事件
 */
static void tab_event_handler(lv_event_t *e) {
    lv_obj_t *btn = lv_event_get_target(e);

    if (btn == tab_date && current_mode != 0) {
        current_mode = 0;
        clear_content_area();
        create_date_mode_content();
    } else if (btn == tab_doctor && current_mode != 1) {
        current_mode = 1;
        clear_content_area();
        create_doctor_mode_content();
    }

    update_tab_style();
}


// -------------------------- 4. 工具函数 --------------------------
/**
 * @brief 清空内容区所有子对象
 */
static void clear_content_area(void) {
    if (lv_obj_is_valid(content_area)) {
        lv_obj_clean(content_area);
    }
}

/**
 * @brief 更新Tab按钮的样式
 */
static void update_tab_style() {
    if (!lv_obj_is_valid(tab_date) || !lv_obj_is_valid(tab_doctor)) return;

    // 重置所有Tab样式
    lv_obj_set_style_bg_color(tab_date, lv_color_white(), 0);
    lv_obj_set_style_text_color(tab_date, lv_color_hex(0x555555), 0);
    lv_obj_set_style_border_color(tab_date, lv_color_hex(0xCCCCCC), 0);
    lv_obj_set_style_border_width(tab_date, 1, 0);

    lv_obj_set_style_bg_color(tab_doctor, lv_color_white(), 0);
    lv_obj_set_style_text_color(tab_doctor, lv_color_hex(0x555555), 0);
    lv_obj_set_style_border_color(tab_doctor, lv_color_hex(0xCCCCCC), 0);
    lv_obj_set_style_border_width(tab_doctor, 1, 0);

    // 高亮当前模式的Tab
    if (current_mode == 0) {
        lv_obj_set_style_bg_color(tab_date, lv_color_hex(0x1E88E5), 0);
        lv_obj_set_style_text_color(tab_date, lv_color_white(), 0);
        lv_obj_set_style_border_width(tab_date, 0, 0);
    } else {
        lv_obj_set_style_bg_color(tab_doctor, lv_color_hex(0x1E88E5), 0);
        lv_obj_set_style_text_color(tab_doctor, lv_color_white(), 0);
        lv_obj_set_style_border_width(tab_doctor, 0, 0);
    }
}


// -------------------------- 5. 内容区生成 --------------------------
/**
 * @brief 创建“按日期挂号”模式的内容
 *        包含日期选择按钮和医生列表
 */
static void create_date_mode_content(void) {
    // 1. 创建日期选择标题
    lv_obj_t *date_title = lv_label_create(content_area);
    lv_label_set_text(date_title, "请选择预约日期：");
    lv_obj_set_style_text_font(date_title, &lv_font_fan_24, 0);
    lv_obj_set_style_text_color(date_title, lv_color_hex(0x333333), 0);
    lv_obj_align(date_title, LV_ALIGN_TOP_LEFT, 30, 20);

    // 2. 创建日期选择按钮组（使用Flex布局）
    lv_obj_t *date_btn_container = lv_obj_create(content_area);
    lv_obj_set_size(date_btn_container, lv_pct(90), 60);
    lv_obj_align(date_btn_container, LV_ALIGN_TOP_MID, 0, 70);
    lv_obj_set_style_bg_opa(date_btn_container, LV_OPA_0, 0);
    lv_obj_set_style_border_width(date_btn_container, 0, 0);
    lv_obj_set_layout(date_btn_container, LV_LAYOUT_FLEX);
    lv_obj_set_flex_flow(date_btn_container, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(date_btn_container, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    // 为每个日期创建一个按钮
    static int date_indices[DATE_COUNT]; // 用于向事件回调传递索引
    for (int i = 0; i < DATE_COUNT; i++) {
        date_indices[i] = i;

        date_buttons[i] = lv_btn_create(date_btn_container);
        lv_obj_set_size(date_buttons[i], 100, 50);
       
        lv_obj_set_style_border_width(date_buttons[i], 1, 0);
        lv_obj_set_style_border_color(date_buttons[i], lv_color_hex(0xCCCCCC), 0);
        
        lv_obj_t *label = lv_label_create(date_buttons[i]);
        lv_label_set_text(label, selectable_dates[i].show_name);
        lv_obj_set_style_text_font(label, &lv_font_fan_24, 0);
        lv_obj_center(label);

        lv_obj_add_event_cb(date_buttons[i], date_btn_event_handler, LV_EVENT_CLICKED, &date_indices[i]);

        // 初始化第一个日期为选中状态
        if (i == 0) {
            lv_obj_set_style_bg_color(date_buttons[i], lv_color_hex(0x1E88E5), 0);
            lv_obj_set_style_text_color(label, lv_color_white(), 0);
            // 同时设置全局选中日期变量
            if (selected_date[0] == '\0') {
                strncpy(selected_date, selectable_dates[i].raw_date, sizeof(selected_date) - 1);
                selected_date[sizeof(selected_date) - 1] = '\0';
            }
        }
    }

    // 3. 加载并显示当前选中日期的医生列表
    populate_date_mode_list(selected_date);
}

/**
 根据指定日期填充医生列表
 */
static void populate_date_mode_list(const char *target_date) {
    if (!target_date || *target_date == '\0') return;

    // 找到用于显示的日期名称（不变）
    const char *show_date = "未知日期";
    for (int i = 0; i < DATE_COUNT; i++) {
        if (strcmp(selectable_dates[i].raw_date, target_date) == 0) {
            show_date = selectable_dates[i].show_name;
            break;
        }
    }

    // 医生列表标题（不变）
    lv_obj_t *title = lv_label_create(content_area);
    char title_buf[50];
    snprintf(title_buf, sizeof(title_buf), "%s 可预约医生", show_date);
    lv_label_set_text(title, title_buf);
    lv_obj_set_style_text_font(title, &lv_font_fan_24, 0);
    lv_obj_set_style_text_color(title, lv_color_hex(0x1E88E5), 0);
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 150);

    // 医生列表容器（不变）
    lv_obj_t *doc_list = lv_list_create(content_area);
    lv_obj_set_size(doc_list, lv_pct(90), lv_pct(60));
    lv_obj_align(doc_list, LV_ALIGN_TOP_MID, 0, 200);
    lv_obj_set_scrollbar_mode(doc_list, LV_SCROLLBAR_MODE_AUTO);

    int doc_count = sizeof(internal_medicine_doctors) / sizeof(DoctorInfo);
    int any_schedule = 0; // 新增：标记整体是否有排班，替代原has_schedule

    for (int i = 0; i < doc_count; i++) {
        int has_schedule = 0; // 核心修复：循环内重置，每次判断当前医生前清零
        DoctorInfo *doc = &internal_medicine_doctors[i];
        char schedule_buf[200] = {0};

        // 检查当前医生在目标日期是否有排班（不变）
        for (int j = 0; j < 3; j++) {
            if (doc->schedules[j].date && 
                strcmp(doc->schedules[j].date, target_date) == 0 && 
                doc->schedules[j].remain_ticket > 0) {
                has_schedule = 1;
                any_schedule = 1; // 标记整体有排班
                snprintf(schedule_buf + strlen(schedule_buf), 
                         sizeof(schedule_buf) - strlen(schedule_buf),
                         "- %s（余票：%d张）\n", 
                         doc->schedules[j].time, doc->schedules[j].remain_ticket);
            }
        }

        // 只有当前医生有排班时，才创建按钮（不变）
        if (has_schedule) {
            lv_obj_t *list_btn = lv_list_add_btn(doc_list, NULL, doc->name);
            lv_obj_set_style_text_font(list_btn, &lv_font_fan_24, 0);
            lv_obj_set_height(list_btn, 180);

            lv_obj_t *sub_label = lv_label_create(list_btn);
            char sub_text[300];
            snprintf(sub_text, sizeof(sub_text),
                     "职称：%s\n简介：%s\n\n当日排班：\n%s",
                     doc->title, doc->intro, schedule_buf);
            lv_label_set_text(sub_label, sub_text);
            lv_obj_set_style_text_font(sub_label, &lv_font_fan_24, 0);
            lv_obj_set_style_text_color(sub_label, lv_color_hex(0x666666), 0);
            lv_obj_align(sub_label, LV_ALIGN_TOP_LEFT, 20, 40);
            lv_label_set_long_mode(sub_label, LV_LABEL_LONG_WRAP);
            lv_obj_set_width(sub_label, lv_pct(80));

            lv_obj_t *reserve_btn = lv_btn_create(list_btn);
            lv_obj_set_size(reserve_btn, 120, 40);
            lv_obj_align(reserve_btn, LV_ALIGN_BOTTOM_RIGHT, -20, -10);
            lv_obj_set_style_bg_color(reserve_btn, lv_color_hex(0x2ECC71), 0);

            lv_obj_t *reserve_label = lv_label_create(reserve_btn);
            lv_label_set_text(reserve_label, "立即预约");
            lv_obj_set_style_text_font(reserve_label, &lv_font_fan_24, 0);
            lv_obj_set_style_text_color(reserve_label, lv_color_white(), 0);
            lv_obj_center(reserve_label);

            // 正确绑定医生姓名（不变）
            lv_obj_set_user_data(reserve_btn, (void *)doc->name);
            lv_obj_add_event_cb(reserve_btn, reserve_btn_event_cb, LV_EVENT_CLICKED, NULL);
        }
    }

    // 用any_schedule判断是否显示空提示（修复：避免原has_schedule判断错误）
    if (!any_schedule) {
        lv_obj_t *empty_label = lv_label_create(content_area);
        lv_label_set_text(empty_label, "当前日期暂无医生排班，请选择其他日期。");
        lv_obj_set_style_text_font(empty_label, &lv_font_fan_24, 0);
        lv_obj_set_style_text_color(empty_label, lv_color_hex(0x999999), 0);
        lv_obj_align(empty_label, LV_ALIGN_CENTER, 0, 0);
    }
}

/**
 * @brief 创建“按医生挂号”模式的内容（保持原样）
 */
static void create_doctor_mode_content(void) {
    clear_content_area();

    // 标题和列表容器（不变）
    lv_obj_t *title = lv_label_create(content_area);
    lv_label_set_text(title, "内科医生列表");
    lv_obj_set_style_text_font(title, &lv_font_fan_24, 0);
    lv_obj_set_style_text_color(title, lv_color_hex(0x1E88E5), 0);
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);

    lv_obj_t *doc_list = lv_list_create(content_area);
    lv_obj_set_size(doc_list, lv_pct(90), lv_pct(80));
    lv_obj_align(doc_list, LV_ALIGN_TOP_MID, 0, 60);
    lv_obj_set_scrollbar_mode(doc_list, LV_SCROLLBAR_MODE_AUTO);

    int doc_count = sizeof(internal_medicine_doctors) / sizeof(DoctorInfo);
    for (int i = 0; i < doc_count; i++) {
        // 医生列表项创建（不变）
        DoctorInfo *doc = &internal_medicine_doctors[i];
        lv_obj_t *list_btn = lv_list_add_btn(doc_list, NULL, doc->name);
        lv_obj_set_style_text_font(list_btn, &lv_font_fan_24, 0);
        lv_obj_set_height(list_btn, LV_SIZE_CONTENT);

        // 医生信息标签（不变）
        lv_obj_t *sub_label = lv_label_create(list_btn);
        char sub_text[200];
        snprintf(sub_text, sizeof(sub_text),
                 "职称：%s\n简介：%s\n\n可预约时段：",
                 doc->title, doc->intro);
        lv_label_set_text(sub_label, sub_text);
        lv_obj_set_style_text_font(sub_label, &lv_font_fan_24, 0);
        lv_obj_set_style_text_color(sub_label, lv_color_hex(0x666666), 0);
        lv_obj_align(sub_label, LV_ALIGN_TOP_LEFT, 20, 40);
        lv_label_set_long_mode(sub_label, LV_LABEL_LONG_WRAP);
        lv_obj_set_width(sub_label, lv_pct(90));

        // 排班信息（不变）
        char schedule_text[300] = {0};
        for (int j = 0; j < 3; j++) {
            if (doc->schedules[j].remain_ticket > 0) {
                snprintf(schedule_text + strlen(schedule_text), sizeof(schedule_text) - strlen(schedule_text),
                         "- %s %s（余票：%d张）\n", doc->schedules[j].date, doc->schedules[j].time, doc->schedules[j].remain_ticket);
            }
        }
        
        if (strlen(schedule_text) == 0) {
            strcpy(schedule_text, "- 暂无可用预约号源");
        }
        lv_label_ins_text(sub_label, LV_LABEL_POS_LAST, schedule_text);

        // 预约按钮创建（不变）
        lv_obj_t *reserve_btn = lv_btn_create(list_btn);
        lv_obj_set_size(reserve_btn, 120, 40);
        lv_obj_align(reserve_btn, LV_ALIGN_BOTTOM_RIGHT, -20, -20);
        lv_obj_set_style_bg_color(reserve_btn, lv_color_hex(0x2ECC71), 0);

        int has_any_ticket = 0;
        for (int j = 0; j < 3; j++) {
            if (doc->schedules[j].remain_ticket > 0) {
                has_any_ticket = 1;
                break;
            }
        }
        if (!has_any_ticket) {
            lv_obj_add_state(reserve_btn, LV_STATE_DISABLED);
            lv_obj_set_style_bg_color(reserve_btn, lv_color_hex(0xCCCCCC), LV_PART_MAIN | LV_STATE_DISABLED);
        }

        lv_obj_t *reserve_label = lv_label_create(reserve_btn);
        lv_label_set_text(reserve_label, "立即预约");
        lv_obj_set_style_text_font(reserve_label, &lv_font_fan_24, 0);
        lv_obj_set_style_text_color(reserve_label, lv_color_white(), 0);
        lv_obj_align(reserve_label, LV_ALIGN_LEFT_MID, -50, 0);

        // 核心修改：删除传参，改为设user_data
        if (has_any_ticket) {
            lv_obj_set_user_data(reserve_btn, (void *)doc->name); // 给按钮绑定医生姓名
            lv_obj_add_event_cb(reserve_btn, reserve_btn_event_cb, LV_EVENT_CLICKED, NULL); // 回调无额外参数
        }
    }
}


// -------------------------- 6. Screen8 初始化与销毁 --------------------------
void ui_Screen8_screen_init(void) {
    if (ui_Screen8) {
        lv_obj_del(ui_Screen8);
        ui_Screen8 = NULL;
    }

    ui_Screen8 = lv_obj_create(NULL);
    lv_obj_clear_flag(ui_Screen8, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_color(ui_Screen8, lv_color_hex(0xFFFFFF), 0);

    // 标题
    lv_obj_t *title_label = lv_label_create(ui_Screen8);
    char title[50];
    snprintf(title, sizeof(title), "%s - 挂号选择", g_selected_dept);
    lv_label_set_text(title_label, title);
    lv_obj_set_style_text_font(title_label, &lv_font_fan_24, 0);
    lv_obj_set_style_text_color(title_label, lv_color_hex(0x1E88E5), 0);
    lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 30);

    // 返回按钮
    lv_obj_t *back_btn = lv_btn_create(ui_Screen8);
    lv_obj_set_size(back_btn, 100, 45);
    lv_obj_align(back_btn, LV_ALIGN_TOP_LEFT, 20, 20);
    lv_obj_add_event_cb(back_btn, back_btn_event_handler, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t *back_btn_label = lv_label_create(back_btn);
    lv_label_set_text(back_btn_label, "返回");
    lv_obj_set_style_text_font(back_btn_label, &lv_font_fan_24, 0);
    lv_obj_center(back_btn_label);

    // Tab栏
    lv_obj_t *tab_bar = lv_obj_create(ui_Screen8);
    lv_obj_set_size(tab_bar, lv_pct(90), 80);
    lv_obj_align(tab_bar, LV_ALIGN_TOP_MID, 0, 80);
    lv_obj_set_layout(tab_bar, LV_LAYOUT_FLEX);
    lv_obj_set_flex_flow(tab_bar, LV_FLEX_FLOW_ROW);
    lv_obj_set_style_bg_opa(tab_bar, LV_OPA_0, 0);

    // Tab - 按日期
    tab_date = lv_btn_create(tab_bar);
    lv_obj_set_size(tab_date, LV_SIZE_CONTENT, LV_PCT(100));
    lv_obj_set_flex_grow(tab_date, 1);
    lv_obj_add_event_cb(tab_date, tab_event_handler, LV_EVENT_CLICKED, NULL);
    lv_obj_t *tab_date_label = lv_label_create(tab_date);
    lv_label_set_text(tab_date_label, "按日期挂号");
    lv_obj_set_style_text_font(tab_date_label, &lv_font_fan_24, 0);
    lv_obj_center(tab_date_label);

    // Tab - 按医生
    tab_doctor = lv_btn_create(tab_bar);
    lv_obj_set_size(tab_doctor, LV_SIZE_CONTENT, LV_PCT(100));
    lv_obj_set_flex_grow(tab_doctor, 1);
    lv_obj_add_event_cb(tab_doctor, tab_event_handler, LV_EVENT_CLICKED, NULL);
    lv_obj_t *tab_doctor_label = lv_label_create(tab_doctor);
    lv_label_set_text(tab_doctor_label, "按医生挂号");
    lv_obj_set_style_text_font(tab_doctor_label, &lv_font_fan_24, 0);
    lv_obj_center(tab_doctor_label);

    // 内容区
    content_area = lv_obj_create(ui_Screen8);
    lv_obj_set_size(content_area, lv_pct(100), lv_pct(70));
    lv_obj_align(content_area, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_set_style_bg_opa(content_area, LV_OPA_0, 0);
    lv_obj_clear_flag(content_area, LV_OBJ_FLAG_SCROLLABLE);

    // 初始化默认模式和日期
    selected_date[0] = '\0'; // 初始化为空，在create_date_mode_content中设置
    current_mode = 0;
    update_tab_style();
    create_date_mode_content();
}

void ui_Screen8_screen_deinit(void) {
    if (ui_Screen8) {
        lv_obj_del(ui_Screen8);
        ui_Screen8 = NULL;
    }
    tab_date = NULL;
    tab_doctor = NULL;
    content_area = NULL;
    // 日期按钮是内容区的子对象，随content_area一起被删除，无需单独处理
}